package com.htthorizon.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import com.htthorizon.model.service.IModelCrossAuditService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Data
@Slf4j
public class ServiceMarket2HTTPUtil {

    @Autowired
    private IModelCrossAuditService modelCrossAuditService;

    private final String host="http://25.212.157.89/openApi/c1f7cfeb54f949dea1545ad08fb8e9150af0ef8c02ae43478f8e400d168ac0ed.apigateway.res.sgmc.sgcc.com.cn";

    private final String groupId="0af0ef8c02ae43478f8e400d168ac0ed";

    private final String appId="1695794288537884";

    public String getMethod(String path,String param){
      return   this.getMethod1(this.host,this.groupId,this.appId,path,param);
    }

    public static String getMethod1(String host,String groupId,String appId,String path,String param) {
            String url=host+path+"?"+param;
        HttpResponse execute = HttpRequest.get(url)
                .header("x-ca-group-id", groupId)
                .header("x-ca-app-id", appId).execute();
        String body = execute.body();


        JSONObject jsonObject = new JSONObject(body);
        Object data = jsonObject.get("data");
        JSONObject entries = new JSONObject(data);
        String invokeInfo = (String) entries.get("invokeInfo");

        String[] split = invokeInfo.split("<br><br><br>");
        String s = split[1];
        return s;
    }

//    public static void main(String[] args) {
//        String host="http://25.212.157.89/openApi/c1f7cfeb54f949dea1545ad08fb8e9150af0ef8c02ae43478f8e400d168ac0ed.apigateway.res.sgmc.sgcc.com.cn";
//        String groupId="0af0ef8c02ae43478f8e400d168ac0ed";
//        String appId="1695794288537884";
//        String path="/CST/SHUZHSJCYHMXEQ/RDS/GET_TOL_AMT_PRE50MF_SF";
//        String param="?pageNum=1&pageSize=10&std_county_org_nm=%E5%A8%84%E6%98%9F&std_city_org_nm=%E5%A8%84%E5%BA%95";
//
//        String method = getMethod(host, groupId, appId, path, param);
//    }

    public  String getParamString(Map<String,Object> params){


        if(params==null || params.size()==0){
            return "";
        }else{
            StringBuilder sb = new StringBuilder();
            String pageNum = params.get("pageNum").toString();
            String pageSize = params.get("pageSize").toString();
            sb.append("pageNum=").append(pageNum).append("&pageSize=").append(pageSize).append("&");
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                if (!(entry.getKey().equals("pageNum"))&&!(entry.getKey().equals("pageSize"))){
                    sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
                }

            }
            return sb.substring(0, sb.length() - 1);
        }
    }

}
